import React from 'react';
import { Select, Radio } from 'antd';

const { Option } = Select;

export function getCookie(name) {
  const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);
  const arr = document.cookie.match(reg);
  if (arr) {
    return decodeURIComponent(arr[2]);
  } else {
    return null;
  }
}

export function delCookie({ name, domain, path }) {
  if (getCookie(name)) {
    document.cookie = `${name}=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=${path}; domain=${domain}`;
  }
}

export function setLocalStorage(key, vaule) {
  return localStorage.setItem(key, JSON.stringify(vaule));
}

export function getLocalStorage(key) {
  const value = JSON.parse(localStorage.getItem(key));
  return value;
}

export function storageClear() {
  sessionStorage.clear();
  localStorage.clear();
}

export function remember(b) {
  localStorage.setItem('REMEMBER', b);
}

export function isRemember() {
  return localStorage.getItem('REMEMBER');
}

export function isLogin() {
  return !!localStorage.getItem('token') || !!sessionStorage.getItem('token');
}

export function putToken({ access_token, refresh_token, expires_in }) {
  if (localStorage.getItem('REMEMBER')) {
    localStorage.setItem('token', access_token);
    localStorage.setItem('refresh_token', refresh_token);
    localStorage.setItem('expires', Date.parse(new Date()) / 1000 + expires_in);
  } else {
    sessionStorage.setItem('token', access_token);
    sessionStorage.setItem('refresh_token', refresh_token);
    sessionStorage.setItem('expires', Date.parse(new Date()) / 1000 + expires_in);
  }
}

export function getToken() {
  return localStorage.getItem('REMEMBER')
    ? localStorage.getItem('token')
    : sessionStorage.getItem('token');
}

export function getExpires() {
  const ex = localStorage.getItem('REMEMBER')
    ? localStorage.getItem('expires')
    : sessionStorage.getItem('expires');
  const date = Date.parse(new Date()) / 1000;
  return ex - date;
}

export function getRefreshToken() {
  return localStorage.getItem('REMEMBER')
    ? localStorage.getItem('refresh_token')
    : sessionStorage.getItem('refresh_token');
}

export function getDicByCode(data, code) {
  return data ? data[code].sort((a, b) => a.sort - b.sort) : [];
}

//更安全的方式。根据数据字典code找对应的[]类型的值。
export const findDicByCode = (data, code) => (data ? data[code] : []);

export function getDataByCode(data, code, key) {
  return data && data[code] ? (data[code].find(d => d.code === key) ? data[code].find(d => d.code === key).name : key) : key;
}

export function getDicByKey(data, code, key) {
  return data && data[code]
    ? data[code].find(d => d.key === key)
      ? data[code].find(d => d.key === key).value
      : key
    : key;
}

export function getDicByKeyValue(data, code, key) {
  return data && data[code]
    ? data[code].find((d) => d.key == key)
      ? data[code].find((d) => d.key == key).value
      : key
    : key;
}

export function getDicByCodeSort(data, code) {
  if (data && data[code]) {
    if (data[code].length !== 0) {
      data[code].sort((a, b) => a.sort - b.sort);
      return data[code].map(d => <Option key={d.key}>{d.value}</Option>);
    } else {
      return [];
    }
  } else {
    return [];
  }
}

export function getDicByBaicList(data, code) {
  if (data && data[code]) {
    if (data[code].length !== 0) {
      data[code].sort((a, b) => a.sort - b.sort);
      return data[code]
        .filter(item => item.key !== '3')
        .map(d => <Option key={d.key}>{d.value}</Option>);
    } else {
      return [];
    }
  } else {
    return [];
  }
}

export function getDicByBaicDealer(data, code) {
  if (data && data[code]) {
    if (data[code].length !== 0) {
      data[code].sort((a, b) => a.sort - b.sort);
      return data[code].map(d => {
        if (d.key === '1') {
          return (
            <Option key={d.key}>
              <span color="red">{d.value}</span>
            </Option>
          );
        } else {
          return <Option key={d.key}>{d.value}</Option>;
        }
      });
    } else {
      return [];
    }
  } else {
    return [];
  }
}

export function getDicByCodeSortRadio(data, code) {
  if (data && data[code]) {
    if (data[code].length !== 0) {
      data[code].sort((a, b) => a.sort - b.sort);
      return data[code].map(d => (
        <Radio key={d.key} value={d.key}>
          {d.value}
        </Radio>
      ));
    } else {
      return [];
    }
  } else {
    return [];
  }
}

export function encryptedData (data) {
  let encryptor = new JSEncrypt();
  encryptor.setPublicKey('MIG/MA0GCSqGSIb3DQEBAQUAA4GtADCBqQKBoQCr8OGuabLmlVKu7OfV2xUlTchNKiT3gDAGs5ICcrdTaZUqXzqyaoe/99hoD5NSXXf88dlBxeaGt1w7kt5kFcxm7+Z8GfWUy7hA3KmZTLAaysZY3Q06kYsNJBHzojE/sVTVmgchFCTUJ3Cy7McYakvV7rlN8Qj6wfJkK9l6966+zcw9Q4chFfS6+5secw3Ia7PxM8oXzUtkpsiCWGudjHJtAgMBAAE=');
  return encryptor.encrypt(data);
}


export function randomPassword() {
  let num = ["1","2","3","4","5","6","7","8","9"];
  let english = ["a","b","c","d","e","f","g","h","j","k","m","n","p","q","r","s","t","u","v","w","x","y","z"];
  let ENGLISH = ["A","B","C","D","E","F","G","H","J","K","M","N","P","Q","R","S","T","U","V","W","X","Y","Z"];
  let config = num.concat(english).concat(ENGLISH);

  let arr = [];
  arr.push(getOne(num));
  arr.push(getOne(english));
  arr.push(getOne(ENGLISH));

  //获取需要生成的长度
  let len = 8;

  for(let i=3; i<len; i++){
    arr.push(config[Math.floor(Math.random()*config.length)]);
  }

  //乱序
  let newArr = [];
  for(let j=0; j<len; j++){
    newArr.push(arr.splice(Math.random()*arr.length,1)[0]);
  }
  return newArr.join("");
}